#!/usr/bin/env python3
import sys
from scapy.all import sniff, hexdump, get_if_list

# 自定义 SR EtherType
TYPE_SR = 0x0908

def get_iface():
    for iface in get_if_list():
        if "eth0" in iface:
            return iface
    raise RuntimeError("Cannot find eth0 interface")

def parse_sr(pkt):
    if pkt.haslayer("Ether") and pkt.type == TYPE_SR:
        print("=== Received SR packet ===")
        pkt.show2()
        # 解析 SR 头
        raw = bytes(pkt.payload)[:1 + pkt.payload.load[0]]
        path_len = raw[0]
        ports = list(raw[1:])
        print(f"SR path_len={path_len}, ports={ports}")
        print("===========================")

def main():
    iface = get_iface()
    print(f"Listening on {iface} for SR EtherType 0x{TYPE_SR:04x}")
    sniff(iface=iface, prn=parse_sr, store=False)

if __name__ == "__main__":
    main()
